package com.egg.auth.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egg.auth.entity.menu.Menu;
import com.egg.auth.model.request.web.MenuPostOrPut;
import com.egg.auth.model.response.web.MenuInfo;
import com.egg.auth.model.response.web.MenuTree;
import com.egg.auth.model.response.web.RouterTree;
import com.egg.auth.service.menu.IMenuService;
import com.egg.common.context.util.LocalThreadContextUtils;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.OptionsResponse;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import com.egg.common.util.OptionalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Api(tags = WebController.API_PREFIX + "菜单中心")
@RestController
@RequestMapping("/menu")
public class WebMenuController extends WebController {


    @Autowired
    private IMenuService menuService;


    @ApiOperation(value = "路由树")
    @GetMapping("/router/tree")
    public ResponseData<List<RouterTree>> routerTreeGet() {
        List<Menu> dbList = menuService.findByUserId(LocalThreadContextUtils.getUserId());
        List<MenuTree> menuTreeList = menuService.convertMenuTree(dbList);
        List<RouterTree> routerTreeList = menuService.convertRouterTree(menuTreeList);
        return ResponseData.success(routerTreeList);
    }

    @ApiOperation(value = "菜单树下拉选")
    @GetMapping(value = "/tree/option")
    public ResponseData<List<OptionsResponse>> menuTreeOptionGet() {
        LambdaQueryWrapper<Menu> qw = menuService.buildLambdaQw();
        qw.orderByAsc(Menu::getSort);
        List<Menu> dbList = menuService.list(qw);
        List<MenuTree> menuTreeList = menuService.convertMenuTree(dbList);
        List<OptionsResponse> voList = convertOptionRequest(menuTreeList);
        return ResponseData.success(voList);
    }

    private List<OptionsResponse> convertOptionRequest(List<MenuTree> menuTreeList) {
        if (ObjectUtil.isEmpty(menuTreeList)) {
            return Collections.emptyList();
        }
        List<OptionsResponse> optionList = new ArrayList<>();
        menuTreeList.forEach(o -> {
            OptionsResponse option = new OptionsResponse();
            option.setValue(o.getId().toString());
            option.setLabel(o.getName());
            option.setChildren(convertOptionRequest(o.getChildren()));
            optionList.add(option);
        });
        return optionList;
    }

    @ApiOperation(value = "菜单新增")
    @PostMapping
    public ResponseData<Long> menuPost(@RequestBody MenuPostOrPut param) {
        Long id = menuService.add(param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "菜单详情")
    @GetMapping("/{id}/info")
    public ResponseData<MenuInfo> menuInfoGet(@ApiParam(value = "菜单ID") @PathVariable String id) {
        Menu menu = menuService.baseGetById(id);
        MenuInfo menuInfo = JsonUtil.objToObj(menu, MenuInfo.class);
        return ResponseData.success(menuInfo);
    }

    @ApiOperation(value = "菜单编辑")
    @PutMapping("/{id}")
    public ResponseData<String> menuPut(@PathVariable String id, @RequestBody MenuPostOrPut param) {
        menuService.edit(id, param);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "菜单树")
    @GetMapping("/tree")
    public ResponseData<List<MenuTree>> menuTreeGet(
            @ApiParam(value = "关键字") @RequestParam(value = "keywords", required = false) String keywords
    ) {
        List<Menu> dbList = menuService.find(OptionalUtil.build(keywords));
        List<MenuTree> menuTreeList = menuService.convertMenuTree(dbList);
        return ResponseData.success(menuTreeList);
    }

    @ApiOperation(value = "菜单删除")
    @DeleteMapping
    public ResponseData<String> menuListDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        menuService.delMenu(
                ids.stream()
                        .filter(o -> ObjectUtil.isNotEmpty(o))
                        .map(o -> Long.valueOf(o))
                        .collect(Collectors.toSet())
        );
        return ResponseData.success();
    }


}
